package com.cc.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @User c.c
 * @Since 2022/8/11
 * @Time 9:51
 * @Description 数据库连接工具
 */

public class JdbcUtil {
  private static String driver;
  private static String url;
  private static String user;
  private static String password;
  private static String dbname;

    //静态块
  static {
      try {
        Properties properties = new Properties();

        InputStream input = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(input);

        dbname = properties.getProperty("dbname");
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        user = properties.getProperty("username");
        password = properties.getProperty("password");

      } catch (IOException e) {
        e.printStackTrace();
      }
    }

  public static Connection getConnection(){
    try {
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url,user,password);
      return connection;

    }catch (Exception e){
      e.printStackTrace();

    }
    return null;
  }

  public static String getDbname() {
    dbname = "'"+dbname+"'";
    return dbname;
  }
}
